<?php
class Zenddn_System_Info{
    
    public function __construct(){
    	$ns = new Zend_Session_Namespace('info');
        //$ns->setExpirationSeconds(3600);
    }
    
    public function createInfo(){
    	$auth = Zend_Auth::getInstance();
        $authInfo = $auth->getIdentity();
        
        $this->setUserInfo($authInfo);
        $this->setUserProfile($authInfo);
        $this->setGroupInfo($authInfo);
        $this->setPermission();
    }
    
    public function destroyInfo(){
    	$ns = new Zend_Session_Namespace('info');
        $ns->unsetAll();
    }
    
    public function setUserInfo($authInfo){
    	$db = Zend_Registry::get('db');
        
        $select = $db->select()
                        ->from(array('u' => 'users'))
                        ->where("id = ?", $authInfo->id, 'INTEGER');
        
        $result = $db->fetchRow($select);
        
        $ns = new Zend_Session_Namespace('info');
        $ns->user = $result;
    }
    
    public function setUserProfile($authInfo){
    	$db = Zend_Registry::get('db');
        
        $select = $db->select()
                        ->from(array('u' => 'users'), array('name'))
                        ->join(array('up' => 'user_profiles'), "up.user_id = u.id", array('profile_id'))
                        ->join(array('p' => 'profiles'), "p.id = up.profile_id", array('id', 'first_name', 'last_name', 'avatar'))
                        ->where("u.id = ?", $authInfo->id, 'INTEGER');
        
        $result = $db->fetchRow($select);
        
        $ns = new Zend_Session_Namespace('info');
        $ns->profile = $result;
    }
    
    public function setGroupInfo($authInfo){
    	$db = Zend_Registry::get('db');
        
        $select = $db->select()
                        ->from(array('ug' => 'user_groups'))
                        ->where("id = ?", $authInfo->user_group, 'INTEGER');
        
        $result = $db->fetchRow($select);
        
        $ns = new Zend_Session_Namespace('info');
        $ns->group = $result;
    }
    
    public function getUserInfo($part = null){
    	$ns = new Zend_Session_Namespace('info');
        
        $info = $ns->user;
        
        if($part != null){
            $info = $info->{$part};
        }
        
        return $info;
    }
    
    public function getUserProfile($part = null){
    	$ns = new Zend_Session_Namespace('info');
        
        $info = $ns->profile;
        
        if($part != null){
            $info = $info->{$part};
        }
        
        return $info;
    }
    
    public function getGroupInfo($part = null){
    	$ns = new Zend_Session_Namespace('info');
        
        $info = $ns->group;
        
        if($part != null){
            $info = $info->{$part};
        }
        
        return $info;
    }
    
    public function getInfo(){
    	$ns = new Zend_Session_Namespace('info');
        $info = $ns->getIterator();
        
        return $info;
    }
    
    public function setPermission(){
    	$acl = new Zenddn_System_Acl();
        $acl->createPrivileges();
        $acl->createRole();
    }
    
    public function getPermission($part = null){
    	$ns = new Zend_Session_Namespace('info');
        
        $info = $ns->acl;
        
        if($part != null){
            $info = $info->{$part};
        }
        
        return $info;
    }
}